package com.wanmei.controller.user;

import com.wanmei.exception.WanmeiException;
import com.wanmei.pojo.JsonResult;
import com.wanmei.pojo.Role;
import com.wanmei.pojo.User;
import com.wanmei.service.RoleService;
import com.wanmei.service.UserService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
public class AddUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    /**
     * 跳转到添加用户界面
     */
    @RequestMapping("/addUser.do")
    public ModelAndView  toAddUser() {
        ModelAndView mv = new ModelAndView();
        List<Role> list =roleService.getAllRoles();
        mv.addObject("list",list);
        mv.setViewName("user/addUser");
        return mv;
    }

    @RequestMapping("/addUserInfo.json")
    @ResponseBody
    public JsonResult addUserInfo(User user, HttpServletRequest req) throws WanmeiException {
        String roleId = req.getParameter("heard");
        user.setRid(Integer.parseInt(roleId));
        JsonResult result = new JsonResult();
        int code = userService.addUserInfo(user, "admin");
        result.setCode(code);
        return result;
    }
}
